import { defineStore } from 'pinia'
import request from '@/utils/request'

export const useArticleStore = defineStore('article', {
  state: () => ({
    articles: [],
    currentArticle: null,
    loading: false,
    total: 0
  }),

  actions: {
    async fetchArticles(page = 1, limit = 10) {
      this.loading = true
      try {
        // 模拟API请求
        const mockData = {
          list: [
            {
              id: 1,
              title: 'Vue3 组合式API最佳实践',
              summary: '探索Vue3组合式API的使用技巧和注意事项...',
              content: '详细内容...',
              date: '2023-08-01'
            },
            {
              id: 2,
              title: '前端性能优化指南',
              summary: '从多个维度提升前端应用性能...',
              content: '详细内容...',
              date: '2023-07-28'
            }
          ],
          total: 100
        }
        
        this.articles = mockData.list
        this.total = mockData.total
      } catch (error) {
        console.error('获取文章列表失败：', error)
      } finally {
        this.loading = false
      }
    },

    async fetchArticleById(id) {
      this.loading = true
      try {
        // 模拟API请求
        this.currentArticle = {
          id,
          title: 'Vue3 组合式API最佳实践',
          content: '详细内容...',
          date: '2023-08-01'
        }
      } catch (error) {
        console.error('获取文章详情失败：', error)
      } finally {
        this.loading = false
      }
    }
  }
}) 